<?php

/**
 * Contacto form base class.
 *
 * @method Contacto getObject() Returns the current form's model object
 *
 * @package    Sanitas
 * @subpackage form
 * @author     Jose Antonio Sánchez Bedoya
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseContactoForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(),
      'nombre'           => new sfWidgetFormInputText(),
      'primer_apellido'  => new sfWidgetFormInputText(),
      'segundo_apellido' => new sfWidgetFormInputText(),
      'num_documento'    => new sfWidgetFormInputText(),
      'tipo_documento'   => new sfWidgetFormInputText(),
      'telefono'         => new sfWidgetFormInputText(),
      'telefono2'        => new sfWidgetFormInputText(),
      'tipo_via'         => new sfWidgetFormInputText(),
      'direccion'        => new sfWidgetFormInputText(),
      'num_via'          => new sfWidgetFormInputText(),
      'resto_via'        => new sfWidgetFormInputText(),
      'piso'             => new sfWidgetFormInputText(),
      'puerta'           => new sfWidgetFormInputText(),
      'escalera'         => new sfWidgetFormInputText(),
      'portal'           => new sfWidgetFormInputText(),
      'municipio'        => new sfWidgetFormInputText(),
      'cod_postal'       => new sfWidgetFormInputText(),
      'provincia'        => new sfWidgetFormInputText(),
      'email'            => new sfWidgetFormInputText(),
      'fecha_nac'        => new sfWidgetFormDate(),
      'sexo'             => new sfWidgetFormInputText(),
      'autoriza'         => new sfWidgetFormInputText(),
      'idioma'           => new sfWidgetFormInputText(),
      'polizasTotales'   => new sfWidgetFormInputText(),
      'polizasBajas'     => new sfWidgetFormInputText(),
      'created_at'       => new sfWidgetFormDateTime(),
      'updated_at'       => new sfWidgetFormDateTime(),
      'validated_at'     => new sfWidgetFormDateTime(),
      'is_validated'     => new sfWidgetFormInputCheckbox(),
      'removed_at'       => new sfWidgetFormDateTime(),
      'remove_it'        => new sfWidgetFormInputCheckbox(),
      'users_list'       => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
      'polizas_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Poliza')),
      'grupos_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Grupo')),
      'my_users_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
    ));

    $this->setValidators(array(
      'id'               => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'nombre'           => new sfValidatorString(array('max_length' => 255)),
      'primer_apellido'  => new sfValidatorString(array('max_length' => 255)),
      'segundo_apellido' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'num_documento'    => new sfValidatorString(array('max_length' => 15)),
      'tipo_documento'   => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'telefono'         => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'telefono2'        => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'tipo_via'         => new sfValidatorString(array('max_length' => 25, 'required' => false)),
      'direccion'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'num_via'          => new sfValidatorInteger(array('required' => false)),
      'resto_via'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'piso'             => new sfValidatorInteger(array('required' => false)),
      'puerta'           => new sfValidatorString(array('max_length' => 25, 'required' => false)),
      'escalera'         => new sfValidatorString(array('max_length' => 25, 'required' => false)),
      'portal'           => new sfValidatorString(array('max_length' => 25, 'required' => false)),
      'municipio'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'cod_postal'       => new sfValidatorInteger(array('required' => false)),
      'provincia'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email'            => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'fecha_nac'        => new sfValidatorDate(array('required' => false)),
      'sexo'             => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'autoriza'         => new sfValidatorPass(array('required' => false)),
      'idioma'           => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'polizasTotales'   => new sfValidatorInteger(array('required' => false)),
      'polizasBajas'     => new sfValidatorInteger(array('required' => false)),
      'created_at'       => new sfValidatorDateTime(),
      'updated_at'       => new sfValidatorDateTime(),
      'validated_at'     => new sfValidatorDateTime(array('required' => false)),
      'is_validated'     => new sfValidatorBoolean(array('required' => false)),
      'removed_at'       => new sfValidatorDateTime(array('required' => false)),
      'remove_it'        => new sfValidatorBoolean(array('required' => false)),
      'users_list'       => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
      'polizas_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Poliza', 'required' => false)),
      'grupos_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Grupo', 'required' => false)),
      'my_users_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('contacto[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Contacto';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['users_list']))
    {
      $this->setDefault('users_list', $this->object->Users->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['polizas_list']))
    {
      $this->setDefault('polizas_list', $this->object->Polizas->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['grupos_list']))
    {
      $this->setDefault('grupos_list', $this->object->Grupos->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['my_users_list']))
    {
      $this->setDefault('my_users_list', $this->object->MyUsers->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveUsersList($con);
    $this->savePolizasList($con);
    $this->saveGruposList($con);
    $this->saveMyUsersList($con);

    parent::doSave($con);
  }

  public function saveUsersList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['users_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Users->getPrimaryKeys();
    $values = $this->getValue('users_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Users', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Users', array_values($link));
    }
  }

  public function savePolizasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['polizas_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Polizas->getPrimaryKeys();
    $values = $this->getValue('polizas_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Polizas', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Polizas', array_values($link));
    }
  }

  public function saveGruposList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['grupos_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Grupos->getPrimaryKeys();
    $values = $this->getValue('grupos_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Grupos', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Grupos', array_values($link));
    }
  }

  public function saveMyUsersList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['my_users_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MyUsers->getPrimaryKeys();
    $values = $this->getValue('my_users_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MyUsers', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MyUsers', array_values($link));
    }
  }

}
